package cn.edu.ctbu.api01.service;


import cn.edu.ctbu.api01.domain.Student;
import cn.edu.ctbu.api01.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service

public class StudentService {

    @Autowired
    private StudentRepository studentRepository;


    public List<Student> findAll() {
        return studentRepository.findAll();
    }

    //        return studentRepository.findById(id).orElse(null);
    public Student findById(int id) {
        return studentRepository.findById(id).orElse(null);

    }

//    public Student findById(Student student) {
//        studentRepository.save(student);
//        return student;
//    }
//    public void delete(Student student){
//        studentRepository.delete(student);
//    }
    public void save(Student student) {
        studentRepository.save(student);
    }
    public Student insert(Student student) {
        studentRepository.save(student);
        return student;
    }
//    public Student update(Student student) {
//        studentRepository.save(student);
//        return student;
//    }

    public void delete(Integer id){
        studentRepository.deleteById(id);
    }

//    public void update(Student id) {
//        studentRepository.save(findById(id));
//
//    }
    @Transactional

    public Student update(Student student) {
        studentRepository.save(student);
        return student;
    }
    public Student findByName(String name) {
        return studentRepository.findByNameLike(name);
    }
}
